

// always requires an idle view 
#include "kb.h"
#include "touch_algorithm.h"
#include "gui.h"

#include "views.h"

#include "xapp_clock.h"



static void initialize(gui_menu_t * menu, void* pvParameter); 
static void display_callback_func(gui_menu_t * menu);
static int display_refresh_func(gui_menu_t * menu);
static bool on_destroy_func(gui_menu_t * menu);

static void on_button(void* pvParameters, gui_menu_t * menu);  
static void on_touch(void* pvParameters, gui_menu_t * menu);
static void on_event_update(void * pvParameters, gui_menu_t * menu); 
//static void local_view_event_handler( uint32_t tag );


static uint16_t text_scroll_0[] = { 0x8fd9,0x662f,0x4e00,0x6761,0x6eda,0x52a8,0x6d88,0x606f,0xff0c,0x662f,0x5426,0x663e,0x793a,0x5b8c,0x6210, };
static uint16_t text_scroll_1[] = { 0x597d,0x7684, };
static uint16_t text_scroll_2[] = { 0x4eca,0x5929,0x5929,0x6c14,0x771f,0x597d,0x554a,0x53ef,0x4ee5,0x6d4b,0x8bd5,0x6d4b,0x8bd5,0x4e86,};

const static ui_widgets_type_t wglTextScroll[] = 
{
    [0] = uiCanvas( 0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ),
    // step icon 
    [1] = uiPictureBoxOverlay( IMG_Pedometer_Icon_050_050_03, 0, 46, 28, 50, 50, COLOR_BLACK),
    // calorie icon
    [2] = uiPictureBoxOverlay( IMG_Pedometer_Icon_050_050_03, 1, 9, 94, 50, 50, COLOR_BLACK),
    // distance icon 
    [3] = uiPictureBoxOverlay( IMG_Pedometer_Icon_050_050_03, 2, 46, 160, 50, 50, COLOR_BLACK ),
    // step value 
    [4] = uiTextScroll(100, 28, 120, 20, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),
    [5] = uiTextScroll(60, 94, 120, 20, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),
    [6] = uiTextScroll(100, 160, 120, 20, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),
};

const static ui_refresh_part_t wglTextScrollPart[] = 
{
    [0] = xRECTANGLE( 100, 28 , 120, 20 ),
    [1] = xRECTANGLE( 60, 94, 120, 20 ),
    [2] = xRECTANGLE( 100, 160, 120, 20 )
};

static const gui_func_t basefunc = 
{
    .init = initialize,
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func
};

gui_view_t viewTextScrollDemo = 
{
    .func = &basefunc,
    
    .parent = NULL,
    .pWidgetlist =  (ui_widgets_type_t*)wglTextScroll ,
    .widget_cnt = WIDGET_COUNT( wglTextScroll ),

    .flag = GUI_VIEW_FLAG_FUNC
};

int16_t text_delta_x; 

//static uint32_t m_idle_timestamp;

/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0
// 指定生命周期，生命周期结束后自动调用 destroy， 测试过程中，可以将Lifetime = 0 使屏幕常亮 
*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 0;
    menu->lifetime = 0; 

    // 记录创建idle view的时间戳，用于按键输入时，检测上一次需要显示的界面
//    m_idle_timestamp = hub_clock_get_ticks();
}

/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static bool on_destroy_func(gui_menu_t * menu)
{
    /*
        set_fsm( fsmIdleBackground ); 
        return false; 
    */
    
    return true; 
}

/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{    
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;

    if( menu->source == GUI_VIEW_REQ_SRC_UPDATE_PART )
    {
        // check value is required update 
        prop.textscrolled.content = NULL;
        prop.textscrolled.len = 0;
        prop.textscrolled.roll_delta_x = -4;
        gui_widget_update_prop( &widgets[4], &prop );

        prop.textscrolled.roll_delta_x = -1;
        gui_widget_update_prop( &widgets[5], &prop );

        prop.textscrolled.roll_delta_x = -1;
        gui_widget_update_prop( &widgets[6], &prop );
    }    
    // create current value
    else 
    {
        prop.textscrolled.content = text_scroll_0;
        prop.textscrolled.len = sizeof(text_scroll_0)/sizeof(text_scroll_0[0]);
        prop.textscrolled.roll_delta_x = 0;
        gui_widget_update_prop( &widgets[4], &prop );

        prop.textscrolled.content = text_scroll_1;
        prop.textscrolled.len = sizeof(text_scroll_1)/sizeof(text_scroll_1[0]);
        prop.textscrolled.roll_delta_x = 0;
        gui_widget_update_prop( &widgets[5], &prop );

        prop.textscrolled.content = text_scroll_2;
        prop.textscrolled.len = sizeof(text_scroll_2)/sizeof(text_scroll_2[0]);
        prop.textscrolled.roll_delta_x = 0;
        gui_widget_update_prop( &widgets[6], &prop );

        if( menu->source == GUI_VIEW_REQ_SRC_CREATE )
        {
            gui_part_t part = GUI_PART_CONFIG( wglTextScrollPart, 100 );
            gui_part_create( &part );
        }
    }
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;

    switch( touch->type )
    {
        case TP_ALGO_EVT_TYPE_CLICK:
        
        break;
        
        case TP_ALGO_EVT_TYPE_SLIDE_R:
        gui_core_view_create_navigate( &viewTextScrollDemo, views_load_current_watchface(), 0, 0, -GUI_LCD_RES_H, 0 );
        break;
        
        default:
        break;
    }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    kb_evt_t * kb = (kb_evt_t*)pvParameters;
    
    switch( kb->type ) 
    {
        case KB_EVT_CLICK:
        gui_core_view_create_navigate( &viewTextScrollDemo, views_load_current_watchface(), 0, 0, -GUI_LCD_RES_H, 0 );
        break; 
           
        case KB_EVT_SHORT_HOLD_READY:

        break;

        default:
            break;
    }
 
}

static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    
}

/* 界面的触摸事件
   tag 是生效的控件id, 用户根据生效的tag判断是哪个控件点击了，进行处理
*/
//static void local_view_event_handler( uint32_t tag )
//{

//}

